#include "Server_Test.h"

#include "server/Server.h"
#include <Poco/Exception.h>
#include <iostream>
/*********************************************************/
std::string ServerTest::name()
{
	return("Server");
}
/*********************************************************/
bool ServerTest::runUnitTests()
{
	bool result = true;

	if ( !cleanStartStopTest() ) result = false;
	if ( !idleStopTest() ) result = false;
	if ( !doubleStartTest() ) result = false;
	if ( !doubleStopTest() ) result = false;

	return( result ) ;
}
/*********************************************************/
bool ServerTest::doubleStopTest()
{
	std::cout << "Running: doubleStopTest()";
	try {
		Server			server;
		server.start();
		server.stop();
		server.stop();

		std::cout << "... success!" << std::endl;
		return( true );
	} catch(Poco::Exception& e) {
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false );
	} catch(...) {
		std::cout << "... Unknown exception caught, trying to stop the server.. Failed!" << std::endl;
		return( false );
	}
}
/*********************************************************/
bool ServerTest::doubleStartTest()
{
	std::cout << "Running: doubleStartTest()";
	try {
		Server			server;
		server.start();
		server.start();
		server.stop();

		std::cout << "... success!" << std::endl;
		return( true );
	} catch(Poco::Exception& e) {
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false );
	} catch(...) {
		std::cout << "... Unknown exception caught, trying to stop the server.. Failed!" << std::endl;
		return( false );
	}
}
/*********************************************************/
bool ServerTest::idleStopTest()
{
	std::cout << "Running: idleStopTest()";
	try {
		Server			server;
		server.stop();

		std::cout << "... success!" << std::endl;
		return( true );
	} catch(Poco::Exception& e) {
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false );
	} catch(...) {
		std::cout << "... Unknown exception caught, trying to stop the server.. Failed!" << std::endl;
		return( false );
	}
}
/*********************************************************/
bool ServerTest::cleanStartStopTest() 
{
	std::cout << "Running: cleanStartStopTest()";
	try {
		Server			server;			// Basic server instance
		server.start();
		server.stop();
	
	} catch(Poco::Exception& e) {
		std::cout << "... Exception caught: " << e.displayText() << "... Failed!" << std::endl;
		return( false);
	} catch(...) {
		std::cout << "... Unknown exception caught, trying to stop the Server... Failed!" << std::endl;
		return( false );
	}
	
	// We had a clean destruction
	std::cout << "... success!" << std::endl;
	return( true );
}
